package com.taichuan.selfcheck.ui.fragment;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taichuan.selfcheck.R;
import com.taichuan.selfcheck.db.DaoManager;
import com.taichuan.selfcheck.entity.TestResultEntity;
import com.taichuan.selfcheck.mvp.presenter.EmptyPresenter;
import com.taichuan.selfcheck.mvp.viewinterface.EmptyInterface;
import com.taichuan.selfcheck.ui.activity.MainActivity;
import com.taichuan.selfcheck.ui.adapter.TestResultAdapter;
import com.taichuan.selfcheck.ui.fragment.base.AppBaseFragment;
import com.taichuan.selfcheck.utils.ResultRecordManager;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;


/**
 * A simple {@link Fragment} subclass.
 */
public class TestResultFragment extends AppBaseFragment<EmptyInterface, EmptyPresenter> implements EmptyInterface {

    @BindView(R.id.checkResult)
    RecyclerView checkResult;
    @BindView(R.id.result_tip)
    TextView mCheckTip;


    SharedPreferences resultSP = ResultRecordManager.getInstance().getResultSp(getContext());

    private TestResultAdapter testResultAdapter = null;

    private List<TestResultEntity> testResultList = new ArrayList<>();
//    private List<String[]> testResultList = new ArrayList<>();

    @Override
    protected boolean isAllowAutoFinishFragment() {
        return false;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void doOnAutoFinishFragment() {
        operateFragmentManagerBack();
    }

    @Override
    protected void getBundleValues(Bundle bundle) {

    }

    @Override
    protected int setContentViewId() {
        return R.layout.fragment_test_result;
    }

    @Override
    protected void initView(View view, Bundle bundle) {

//        Map<String,String> treeMap = new TreeMap<>(
//                new Comparator<String>() {
//                    @Override
//                    public int compare(String s, String t1) {       //按照功能序号进行排序
//                        String s_arr[] = s.split("\\.");
//                        String t1_arr[] = t1.split("\\.");
//                        return s_arr[0].compareTo(t1_arr[0]); //从小到大进行排序，s_arr[0]<t1_arr[0]
//                    }
//                }
//        );

//        treeMap = (Map<String, String>) resultSP.getAll();
//        treeMap.putAll((Map<String, String>) resultSP.getAll());        //先实现排序接口，再存入数据
//        Iterator<String> iterator = treeMap.keySet().iterator();        //获取SP的key集合
//
//        while (iterator.hasNext()){     //遍历SP的key值
//            String tempKey = iterator.next();       //获取当前的key值
//            testResultList.add(new String[]{tempKey,resultSP.getString(tempKey,"未测试")});        //存入用于传入适配器的list
//        }


        try {
            testResultList= DaoManager.getInstance().getDaoSession().getTestResultEntityDao().loadAll();
        }catch (Exception e){
            e.printStackTrace();
            mCheckTip.setVisibility(View.VISIBLE);
        }
        if(testResultList.size()==0){
            mCheckTip.setVisibility(View.VISIBLE);
        }else{
            mCheckTip.setVisibility(View.GONE);
            testResultAdapter = new TestResultAdapter(testResultList);

            checkResult.setLayoutManager(new LinearLayoutManager(getContext()));
            checkResult.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));

            checkResult.setAdapter(testResultAdapter);
            checkResult.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected EmptyPresenter createPresenter() {
        return new EmptyPresenter();
    }

}
